﻿using System;
using System.Collections.Generic;
using System.Text;
using AGHFiraPlayer.Exceptions;
using AGHFiraPlayer.Strategies.StrategiesImpl;

namespace AGHFiraPlayer.Strategies
{
    class StrategyPool
    {
        private readonly int MAX_AMOUNT;
        private List<IStrategy> strategies = new List<IStrategy>();
        private Boolean isPermitted = true;
        private IStrategy strategy;

        public StrategyPool(Type strategyType, AGHDriver driver)
        {
            if (typeof(GoalKeeperStrategy).IsAssignableFrom(strategyType) || typeof(SimpleShotStrategy).IsAssignableFrom(strategyType) ||
                typeof(SimplePassStrategy).IsAssignableFrom(strategyType))
            {
                MAX_AMOUNT = 1;
            } else {
                MAX_AMOUNT = 4;
            }

            for (int i = 0; i < MAX_AMOUNT; i++)
            {
                //Type tt = Type.GetType(strategyType.FullName);
                //zamiast strategyType dac tt
                Object[] param = new Object[1] { driver };
                IStrategy strategy = (IStrategy)Activator.CreateInstance(strategyType, param);
                strategies.Add(strategy);
            }
        }

        internal void permit() {isPermitted = true;}
        internal void forbid() {isPermitted = false;}

        public IStrategy getFromPoolIfAllowed()
        {
            if (isPermitted)
                return getFromPool();

            return null;
        }

        public IStrategy getFromPool()
        {
            if (strategies.Count == 0)
                return null;

            strategy = strategies[0];
            strategies.RemoveAt(0);
            return strategy;
        }

        public void returnToPool(IStrategy strategy)
        {
            if (strategies.Count == MAX_AMOUNT)
                throw new ProhibitedOperationException("Capacity of the pool exceeded!");

            strategies.Add(strategy);
        }
    }
}
